/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.utilities.Utilities;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class OtherController implements Serializable {

    private String customerID = "";

    public String getCustomerID() {
        return customerID;
    }

    public void setCustomerID(String customerID) {
        this.customerID = customerID;
    }

    public String getLinkWeb() {
        String url = "jdbc:jtds:sqlserver://192.168.1.3/HVTTGSMApp_HVTT";
        Connection connection = null;
        String result = "";
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            connection = DriverManager.getConnection(url, "sa", "20052707");//AR_KhachHang
            String sql = "select LinkWeb,PortWeb from AR_KhachHang where PortWeb=?";
            try (PreparedStatement statement = connection.prepareStatement(sql)) {
                statement.setString(1, StringUtils.split(getCustomerID(), "-")[0]);
                ResultSet rs = statement.executeQuery();
                while (rs.next()) {
                    result = rs.getString("LinkWeb");
                    break;
                }
                statement.close();
            }

        } catch (ClassNotFoundException | SQLException e) {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(OtherController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(OtherController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }
    private String khachHangID = "";
    private String nguoiDD = "";
    private String tenKH = "";
    private String diaChi = "";
    private String dienThoai = "";
    private String email = "";
    private String website = "";
    private String yeuCau="";

    public String getKhachHangID() {
        return khachHangID;
    }

    public void setKhachHangID(String khachHangID) {
        this.khachHangID = khachHangID;
    }

    public String getNguoiDD() {
        return nguoiDD;
    }

    public void setNguoiDD(String nguoiDD) {
        this.nguoiDD = nguoiDD;
    }

    public String getTenKH() {
        return tenKH;
    }

    public void setTenKH(String tenKH) {
        this.tenKH = tenKH;
    }

    public String getDiaChi() {
        return diaChi;
    }

    public void setDiaChi(String diaChi) {
        this.diaChi = diaChi;
    }

    public String getDienThoai() {
        return dienThoai;
    }

    public void setDienThoai(String dienThoai) {
        this.dienThoai = dienThoai;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public void setYeuCau(String yeuCau) {
        this.yeuCau = yeuCau;
    }

    public String getYeuCau() {
        return yeuCau;
    }

    public void reset(ActionEvent event) {
        khachHangID = "";
        tenKH = "";
        nguoiDD = "";
        dienThoai = "";
        diaChi = "";
        email = "";
        website = "";
    }

    public void save(ActionEvent event) {
        if (StringUtils.isBlank(getNguoiDD())
                || StringUtils.isBlank(getDienThoai())
                || StringUtils.isBlank(getEmail())
                || StringUtils.isBlank(getDiaChi())) {//registry-use-master
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status", "Dữ liệu không đúng. Xin vui lòng điền đầy đủ thông tin (*)!");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            RequestContext.getCurrentInstance().execute("validateForm('registry-use-master','valiRequired');");
        } else {
            String url = "jdbc:jtds:sqlserver://192.168.1.3/HVTTGSMApp_HVTT";
            Connection connection = null;
            try {
                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                connection = DriverManager.getConnection(url, "sa", "20052707");//AR_KhachHang
                String sql = "insert into CM_KhachHang(KhachHangID,TenKH,DiaChi,DienThoai,Email,Website,NguoiDD,User_Add,User_Up,Scrn_Add,Scrn_Up,Date_Add,Date_Up,NgayDK,ChiNhanhID,YeuCau) values(?,?,?,?,?,?,?,?,?,?,?,GETDATE(),GETDATE(),?,?,?)";
                try (PreparedStatement statement = connection.prepareStatement(sql)) {
                    String cusID = String.valueOf(RandomStringUtils.randomAlphanumeric(20));
                    statement.setString(1, cusID);
                    statement.setString(2, getTenKH());
                    statement.setString(3, getDiaChi());
                    statement.setString(4, getDienThoai());
                    statement.setString(5, getEmail());
                    statement.setString(6, getWebsite());
                    statement.setString(7, getNguoiDD());
                    statement.setString(8, "WEB");
                    statement.setString(9, "WEB");
                    statement.setString(10, "WEB");
                    statement.setString(11, "WEB");
                    statement.setDate(12, new Date(Calendar.getInstance().getTimeInMillis()));
                    statement.setString(13,"00");
                    statement.setString(14,getYeuCau());
                    boolean b = statement.execute();
                    FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message Status", "Dữ liệu của bạn đã được gửi đi. Chúng tôi sẽ liên hệ với bạn trong thời gian sớm nhất.");
                    FacesContext.getCurrentInstance().addMessage(null, msg);
                    statement.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                RequestContext.getCurrentInstance().execute("PF('dlg').hide()");
            } catch (ClassNotFoundException | SQLException e) {
                try {
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(OtherController.class.getName()).log(Level.SEVERE, null, ex);
                }
            } finally {
                try {
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(OtherController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }

    public void sendCustomerCode(ActionEvent event) {
        if (StringUtils.isBlank(getCustomerID())) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status", "Mã khách hàng không đúng!");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            RequestContext.getCurrentInstance().execute("validateForm('form_validate_customer','valiRequired');");
        } else {
            String linkWeb = getLinkWeb();
            if (StringUtils.isBlank(linkWeb)) {
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status", "Dữ liệu của bạn không tồn tại. Xin vui lòng liên hệ với chúng tôi để sử dụng hệ thống.");
                FacesContext.getCurrentInstance().addMessage(null, msg);
            } else {
                RequestContext.getCurrentInstance().execute("PF('dlg1').hide();");
                Utilities.redirect(linkWeb);
            }
        }
    }
}
